<?php
/**
 * Create a new Survey - Main Screen
 * 
 * @author	Xavier VILAIN
 * @package Nawa\Survey
 * @license #license-nawa-surver#
 * 
 */

use Nawa\MedicalStudy\MedicalCenter\Monitoring\Type;
use Fox\Generator;
use Nawa\Survey;
use Fox\Html\Screen;

$xview = Xview::instance();
//@TODO add authorization check here
$screen = new Screen('sSurver','create');
$screen->pageTitle(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_CREATE));
// Header Block
$blHeader = $xview->component('survey','header');
$screen->add($blHeader);

if($screen->submitted()) {
	$fields = $screen->get();
	if(!$screen->errors()) {
		try {
			$survey = new Survey();
			$fields['category'] = 1;
			$fields['type'] = 1;
			$survey->setFields($fields);
			$survey->create();
			$id = $survey->getId();
			// Add the Monitoring to the MedicalStudy List 
			// TODO make this choice a droplist one in the creation screen or an attachement system
			$type = new Type();
			$type->setFields(array('surveyid' => $id,'active' => 1,'standard' => 0));
			$type->create();
			Fox\Message::Success(Msg(Nawa\Messages::BUNDLE,Nawa\Messages::SURVEY_HAS_BEEN_CREATED,$survey->label()));
			$screen->redirect(Generator::url('survey','update',null,array('surveyid' => $id)));
		} catch (Fox\Object\Exception\Save $e) {
			Fox\Message::Error($e);
		}
	}
} 
echo $screen->display();